﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ESOL.Common.Entity;
using ESOL.WinUI.UICommon;
using ESOL.Business.Mst;
using ESOL.Business.Inv;
using ESOL.Common;

namespace ESOL.WinUI.Inv
{
    public partial class frmManageInvStorage : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        List<string> Listcondition = new List<string>();
        InvStorageEntity mInvStorage;
        #endregion

        #region Constructor
        public frmManageInvStorage()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(repositoryItemGridLookUpEdit1, "UNIT", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(repositoryItemGridLookUpEdit2, "UNIT", true);
            List<MstWarehouseEntity> list = MstWarehouseBs.GetMstWarehouseAllList();
            MstWarehouseEntity entity = new MstWarehouseEntity();
            list.Insert(0, entity);
            cmbWareHouse.DataSource = list;
            cmbWareHouse.DisplayMember = "WarehouseName";
            cmbWareHouse.ValueMember = "WarehouseCode";
        }

        private string GetCondition()
        {
            string condition = "";

            if (string.IsNullOrEmpty(cmbWareHouse.Text) == false && cmbWareHouse.SelectedValue != null)
            {
                condition += " AND D.WAREHOUSE_CODE = '" + cmbWareHouse.SelectedValue.ToString() + "'";
            }
            if (string.IsNullOrEmpty(txtLocationCode.Text) == false)
            {
                condition += " AND C.LOCATION_CODE LIKE '%" + txtLocationCode.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtClass1.Text) == false && string.IsNullOrEmpty(txtClass2.Text) == true)
            {
                condition += " AND B.PRODUCT_CODE LIKE '%" + txtClass1.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtClass1.Text) == true && string.IsNullOrEmpty(txtClass2.Text) == false)
            {
                condition += " AND B.PRODUCT_CODE LIKE '%" + txtClass2.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtClass1.Text) == false && string.IsNullOrEmpty(txtClass2.Text) == false)
            {
                condition += " AND B.PRODUCT_CODE LIKE '%" + txtClass1.Text + txtClass2.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtProductCode.Text) == false)
            {
                condition += " AND B.PRODUCT_CODE LIKE '%" + txtProductCode.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtProductName.Text) == false)
            {
                condition += " AND B.PRODUCT_NAME LIKE '%" + txtProductName.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtSafeQty.Text) == false)
            {
                condition += " AND A.FIXED_QTY LIKE '%" + txtSafeQty.Text + "%'";
            }
            return condition;
        }

        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "B.PRODUCT_CODE";
            DataTable dt = InvStorageBs.GetByCondition(condition, orderBy, rowCount);
            gvMain.DataSource = dt;
        }
        #endregion

        private void frmManageInvStorage_Load(object sender, EventArgs e)
        {
            BindComboBox();
            BindDataGrid();
            btnNew.Enabled = false;
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mInvStorage = new InvStorageEntity(dr);

        }
        protected override void Click_Query()
        {
            BindDataGrid();
        }

        private void txtLocationCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            string sWareHouseCode = cmbWareHouse.SelectedValue.ToString();
            frmMaintainLocationQuery frm = new frmMaintainLocationQuery();
            frm.SWareHouseCode = sWareHouseCode;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                txtLocationCode.Text = frm.SelectLocation.LocationCode;
                txtLocationCode.Tag = frm.SelectLocation;
            }
            else
            {
                txtLocationCode.Text = "";
                txtLocationCode.Tag = null;
            }
        }

        private void txtClass1_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            
        }

        private void txtClass2_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {

        }

        private void btnInHouseHistory_Click(object sender, EventArgs e)
        {
            frmMaintainStorageChangeQuery frm = new frmMaintainStorageChangeQuery();
            frm.sChangeType = "INVC02";
            frm.mSelectInvStorage = mInvStorage;
            frm.ShowDialog();
        }

        private void btnOutHouseHistory_Click(object sender, EventArgs e)
        {
            frmMaintainStorageChangeQuery frm = new frmMaintainStorageChangeQuery();
            frm.sChangeType = "INVC01";
            frm.mSelectInvStorage = mInvStorage;
            frm.ShowDialog();
        }

        private void btnSetSafeQty_Click(object sender, EventArgs e)
        {
            if (mInvStorage == null)
            {
                MessageBox.Show("请选择一个物品！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainSetFixedQty frm = new frmMaintainSetFixedQty();
            frm.SelectedStorage = mInvStorage;
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        private void devButton1_Click(object sender, EventArgs e)
        {
            if (mInvStorage == null)
            {
                MessageBox.Show("请选择一个物品！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainStorageChange frm = new frmMaintainStorageChange();
            frm.SelectedInvStorage = mInvStorage;
            frm.ThisActionType = ActionType.New;
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                BindDataGrid();
            }
        }



    }
}